<template>
	<view class="drugPayment-wrap" v-if="recipeForm">
		<view class="drugPayment-address">
			<u-title value="收货地址"></u-title>

			<view class="drugPayment-address-info" @click="gotoRouter('/pages-member/addressList/addressList?type=1')">
				<view class="drugPayment-address-item">
					<view v-if="userAddress" class="word3 nowrap">收货人：{{ userAddress.name }}&nbsp;&nbsp;{{ userAddress.phone }}</view>
					<view v-if="userAddress" class="txt1 nowrap">
						收货地址：{{ userAddress.province != userAddress.city ? userAddress.province : '' }}{{ userAddress.city }}{{ userAddress.district
						}}{{ userAddress.address }}
					</view>
					<view v-if="!userAddress" class="txt2">请添加收货地址</view>
				</view>

				<image src="http://k8s.vendingtech.vip/images/next_icon@2x.png" class="drugPayment-address-icon"></image>
			</view>
		</view>

		<view class="drugPayment-Rp">
			<u-title value="药品信息"></u-title>
			<view class="prescInfo-drug-list">
				<u-show-goods v-for="(item, index) in recipeForm.recipeDetailDTO" :showRecipe="true" type="0" :data="item" :key="index"></u-show-goods>
			</view>
		</view>

		<u-bottom-button value="立即支付" type="1" content="合计" :num="totalPrice" unit="元" @click="createGoodsOrder"></u-bottom-button>
	</view>
</template>

<script>
import uTitle from '@/components/u-title/u-title.vue';
import uBottomButton from '@/components/u-bottom-button/u-bottom-button.vue';
import uShowGoods from '@/components/u-show-goods/u-show-goods.vue';
import { mapState, mapMutations } from 'vuex';
export default {
	components: {
		uBottomButton,
		uTitle,
		uShowGoods
	},
	data() {
		return {
			recipeId: null,
			recipeForm: null,
			commodityBuyList: [],
			totalPrice: 0,
			payFlag: true,
			orderId: null
		};
	},
	onLoad(options) {
		this.recipeId = options.recipeId;
		this.getUserRecipeDetail();
		if (!this.userAddress) {
			this.addressList();
		}
	},
	computed: {
		...mapState({
			userAddress: state => state.userAddress
		})
	},
	onShow() {},
	methods: {
		...mapMutations({
			setUserAddress: 'setUserAddress'
		}),

		//地址列表
		addressList() {
			this.$api.addressList({}).then(res => {
				if (res.code == 200) {
					this.setUserAddress(res.result[0]);
				} else {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
				}
			});
		},

		//处方详情
		getUserRecipeDetail() {
			this.$api
				.getUserRecipeDetail({
					params: {
						id: this.recipeId
					}
				})
				.then(res => {
					if (res.code == 200) {
						res.result.recipeDetailDTO.forEach((key, index) => {
							key.commodity.buyNum = key.commodityNum;
							res.result.recipeDetailDTO[index] = {
								...key.commodity,
								...key
							};
						});
						this.recipeForm = res.result;

						//生成商品详情
						this.setCommodityBuyList();
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},

		//生成商品详情
		setCommodityBuyList() {
			this.recipeForm.recipeDetailDTO.forEach(key => {
				this.commodityBuyList.push({
					buyNum: key.commodityNum,
					commodityId: key.commodityId
				});
				this.totalPrice += key.commodityNum * key.commodity.price;
			});
		},

		//创建商品订单
		createGoodsOrder() {
			if (!this.userAddress?.id) {
				uni.showToast({
					title: '请先选择收货地址',
					icon: 'none'
				});
				return
			}
			this.gotoRouter('/pages-mall/orderCreate/orderCreate?orderCode=' + this.recipeForm.drugOrderCode);
			// if (this.payFlag) {
			// 	this.payFlag = false;
			// 	this.$api
			// 		.createGoodsOrder({
			// 			data: {
			// 				commodityBuyList: this.commodityBuyList,
			// 				orderType: 3,
			// 				inquiryType: 2,
			// 				userAddressId: this.userAddress.id,
			// 				userRecipeId: this.recipeId
			// 			}
			// 		})
			// 		.then(res => {
			// 			if (res.code == 200) {
			// 				this.payFlag = true;
			// 				this.orderId = res.result.id;
			// 				this.submitOrder();
			// 			} else {
			// 				uni.showToast({
			// 					title: res.message,
			// 					icon: 'none'
			// 				});
			// 			}
			// 		});
			// }
		},

		//发起微信支付
		submitOrder() {
			this.$api
				.submitOrder({
					params: {
						commodityName: '购药订单',
						orderId: this.orderId
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.wxRequestPayment(res.result);
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},

		//微信小程序拉起支付
		wxRequestPayment(res) {
			wx.requestPayment({
				timeStamp: res.timeStamp,
				nonceStr: res.nonceStr,
				package: res.packageValue,
				signType: res.signType,
				paySign: res.paySign,
				success: success => {
					uni.showToast({
						title: '支付成功',
						icon: 'none'
					});
				},
				fail: error => {
					uni.showToast({
						title: '取消支付',
						icon: 'none'
					});
				},
				complete: () => {
					this.payFlag = true;
					this.setUserAddress(null);

					uni.reLaunch({
						url: '/pages-order/drugOrderInfo/drugOrderInfo?id=' + this.orderId
					});
				}
			});
		}
	}
};
</script>

<style lang="less">
@import './drugPayment.less';
</style>
